
package com.android.formula.utils;

import java.util.HashSet;
import java.util.Set;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class PreferenceUtil {

    private static final String TAG = "PreferenceUtil";

    public static final String KEY_USER_AGREEMENT = "KEY_USER_AGREEMENT";

    public static final String KEY_RATE_APP = "KEY_RATE_APP";

    public static final String KEY_LAUNCH_COUNT = "KEY_LAUNCH_COUNT";

    public static final String KEY_DATE_FIRST_LAUNCH = "KEY_DATE_FIRST_LAUNCH";

    public static boolean getBoolean(Context con, String key, boolean defValue) {
        try {
            LogUtils.logInfo(TAG, "PrefUtils : getBoolean : key = " + key);
            return pref(con).getBoolean(key, defValue);
        } catch (Exception e) {
            LogUtils.logError(TAG, e.toString());
            return defValue;
        }
    }

    public static int getInt(Context con, String key, int defValue) {
        try {
            LogUtils.logInfo(TAG, "PrefUtils : getInt : key = " + key);
            return pref(con).getInt(key, defValue);
        } catch (Exception e) {
            LogUtils.logError(TAG, e.toString());
            return defValue;
        }
    }

    public static String getString(Context con, String key, String defValue) {
        try {
            LogUtils.logInfo(TAG, "PrefUtils : getInt : key = " + key);
            return pref(con).getString(key, defValue);
        } catch (Exception e) {
            LogUtils.logError(TAG, e.toString());
            return defValue;
        }
    }

    public static long getLong(Context con, String key, long defValue) {
        try {
            LogUtils.logInfo(TAG, "PrefUtils : getLong : key = " + key);
            return pref(con).getLong(key, defValue);
        } catch (Exception e) {
            LogUtils.logError(TAG, e.toString());
            return defValue;
        }
    }

    public static float getFloat(Context con, String key, float defValue) {
        try {
            LogUtils.logInfo(TAG, "PrefUtils : getFloat : key = " + key);
            return pref(con).getFloat(key, defValue);
        } catch (Exception e) {
            LogUtils.logError(TAG, e.toString());
            return defValue;
        }
    }

    public static Set<String> getStringSet(Context con, String key, Set<String> defValue) {
        try {
            LogUtils.logInfo(TAG, "PrefUtils : getStringSet : key = " + key);
            HashSet<String> ret = null;
            Set<String> set = pref(con).getStringSet(key, null);
            if (set != null) {
                ret = new HashSet<String>(set);
            }
            return ret;
        } catch (Exception e) {
            LogUtils.logError(TAG, e.toString());
            return defValue;
        }
    }

    public static void writeString(Context con, String key, String value) {
        try {
            LogUtils.logInfo(TAG, "PrefUtils : writeString : key = " + key + ", value = " + value);
            pref(con).edit().putString(key, value).commit();
        } catch (Exception e) {
            LogUtils.logError(TAG, e.toString());
        }
    }

    public static void writeBoolean(Context con, String key, boolean value) {
        try {
            LogUtils.logInfo(TAG, "PrefUtils : writeBoolean : key = " + key + ", value = " + value);
            pref(con).edit().putBoolean(key, value).commit();
        } catch (Exception e) {
            LogUtils.logError(TAG, e.toString());
        }
    }

    public static void writeInt(Context con, String key, int value) {
        try {
            LogUtils.logInfo(TAG, "PrefUtils : writeInt : key = " + key + ", value = " + value);
            pref(con).edit().putInt(key, value).commit();
        } catch (Exception e) {
            LogUtils.logError(TAG, e.toString());
        }
    }

    public static void writeLong(Context con, String key, long value) {
        try {
            LogUtils.logInfo(TAG, "PrefUtils : writeLong : key = " + key + ", value = " + value);
            pref(con).edit().putLong(key, value).commit();
        } catch (Exception e) {
            LogUtils.logError(TAG, e.toString());
        }
    }

    public static void writeFloat(Context con, String key, float value) {
        try {
            LogUtils.logInfo(TAG, "PrefUtils : writeFloat : key = " + key + ", value = " + value);
            pref(con).edit().putFloat(key, value).commit();
        } catch (Exception e) {
            LogUtils.logError(TAG, e.toString());
        }
    }

    public static void writeSet(Context con, String key, Set<String> set) {
        try {
            LogUtils.logInfo(TAG, "PrefUtils : writeSet : key = " + key);
            pref(con).edit().putStringSet(key, set).commit();
        } catch (Exception e) {
            LogUtils.logError(TAG, e.toString());
        }
    }

    public static void remove(Context con, String key) {
        try {
            LogUtils.logInfo(TAG, "PrefUtils : remove : key = " + key);
            pref(con).edit().remove(key).commit();
        } catch (Exception e) {
            LogUtils.logError(TAG, e.toString());
        }
    }

    private static SharedPreferences pref(Context con) {
        return PreferenceManager.getDefaultSharedPreferences(con);
    }
}
